package com.hk.commons.utils.sys;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtils {

    /**
     * 返回两个日期的间隔，去除时分秒
     * @param a
     * @param b
     * @return
     */
    public static int getDayBetween(Calendar a,Calendar b){
    	int days=0;
    	Calendar daya=Calendar.getInstance();
        daya.set(a.get(Calendar.YEAR), a.get(Calendar.MONTH), a.get(Calendar.DAY_OF_MONTH));
         
        Calendar dayb=Calendar.getInstance();
        dayb.set(b.get(Calendar.YEAR), b.get(Calendar.MONTH), b.get(Calendar.DAY_OF_MONTH));
         
        days=Math.abs(daya.get(Calendar.DAY_OF_YEAR)-dayb.get(Calendar.DAY_OF_YEAR));
        
    	return days;
    }
    /**
     * 得到指定时间前n天的Calendar
     * @param c
     * @param n
     * @return
     */
    public static Calendar beforeNDays(Calendar c, int n){
        //偏移量，给定n天的毫秒数
        long offset = n*24*60*60*1000;
        Calendar calendar = null;
        if(c != null){
            calendar = c;
        }else{
            calendar = Calendar.getInstance();
        }
        
        calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
        return calendar;
    }
    
    /**
     * 得到指定或者当前时间后n天的Calendar
     * @param c
     * @param n
     * @return
     */
    public static Calendar afterNDays(Calendar c, int n){
        //偏移量，给定n天的毫秒数
        long offset = n*24*60*60*1000;
        Calendar calendar = null;
        if(c != null){
            calendar = c;
        }else{
            calendar = Calendar.getInstance();
        }
        
        calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
        return calendar;
    }
    
    /**
     * 当前时间的昨天时间
     * @param c
     * @return
     */
    public static Calendar yesterday(Calendar c){
        long offset = 1*24*60*60*1000;
        Calendar calendar = null;
        if(c != null){
            calendar = c;
        }else{
            calendar = Calendar.getInstance();
        }
        
        calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
        return calendar;
    }
    
    /**
     * 当前时间的明天时间
     * @param c
     * @return
     */
    public static Calendar tomorrow(Calendar c){
        long offset = 1*24*60*60*1000;
        Calendar calendar = null;
        if(c != null){
            calendar = c;
        }else{
            calendar = Calendar.getInstance();
        }
        
        calendar.setTimeInMillis(calendar.getTimeInMillis() + offset);
        return calendar;
    }
    

    
    /**
     * 日期格式化
     * @param c
     * @param pattern
     * @return
     */
    public static String calendarToYYYYMMDD(Calendar c, String pattern){
        Calendar calendar = null;
        if(c != null){
            calendar = c;
        }else{
            calendar = Calendar.getInstance();
        }
        if(pattern == null || pattern.equals("")){
            pattern = "yyyy年MM月dd日 HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        
        return sdf.format(calendar.getTime());
    }
    
    /**
     * Date类型转换到Calendar类型
     * @param d
     * @return
     */
    public static Calendar Date2Calendar(Date d){
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return c;
    }
    
    /**
     * Calendar类型转换到Date类型
     * @param c
     * @return
     */
    public static Date Calendar2Date(Calendar c){
        return c.getTime();
    }
    
    /**
     * 得到当前时间的字符串表示
     * 格式2010-02-02 12:12:12
     * @return
     */
    public static String getNowTimeByYYYYMMDD(){
        return calendarToYYYYMMDD(Calendar.getInstance(), "yyyy-MM-dd HH:mm:ss");
    }
    
    /**
     * 标准日期格式字符串解析成Calendar对象
     * @param s
     * @return
     */
//    public static Calendar str2Calendar(String str){
//    	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
//    	Date date=new Date();
//		try {
//			date = sdf.parse(str);
//		} catch (ParseException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//    	Calendar calendar = Calendar.getInstance();
//    	calendar.setTime(date);
//    }
    
    
//  /**
//  * 得到指定或者当前时间前offset毫秒的Calendar
//  * @param c
//  * @param offset
//  * @return
//  */
// public static Calendar before(Calendar c, long offset){
//     Calendar calendar = null;
//     if(c != null){
//         calendar = c;
//     }else{
//         calendar = Calendar.getInstance();
//     }
//     
//     calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
//     return calendar;
// }
// 
// /**
//  * 得到指定或者当前时间前offset毫秒的Calendar
//  * @param c
//  * @param offset
//  * @return
//  */
// public static Calendar after(Calendar c, long offset){
//     Calendar calendar = null;
//     if(c != null){
//         calendar = c;
//     }else{
//         calendar = Calendar.getInstance();
//     }
//     
//     calendar.setTimeInMillis(calendar.getTimeInMillis() - offset);
//     return calendar;
// }
//  /**
//  * Date类型转换到Timestamp类型
//  * @param d
//  * @return
//  */
// public static Timestamp Date2Timestamp(Date d){
//     return new Timestamp(d.getTime());
// }
// 
// /**
//  * Calendar类型转换到Timestamp类型
//  * @param c
//  * @return
//  */
// public static Timestamp Calendar2Timestamp(Calendar c){
//     return new Timestamp(c.getTimeInMillis());
// }
// 
}